package com.sse.action.emp;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.sse.common.PageView;
import com.sse.po.dep.Department;
import com.sse.po.emp.Employee;
import com.sse.service.dep.DepServiceFinder;
import com.sse.service.emp.EmpService;
import com.sse.service.emp.EmpServiceFinder;

public class EmpAction {
	
	@Resource(name = "depServiceFinderImpl")
	private DepServiceFinder dfr;
	@Resource(name = "empServiceFinderImpl")
	private EmpServiceFinder efr;
	
	@Resource(name = "empServiceImpl")
	private EmpService ef;
	//上传的文件
	private File image;
	//上传文件的名称
	private String imageFileName;
	
	private Employee emp = new Employee();
	//部门ID
	private String depId;
	
	//查询条件
	private String job;
	private String name;
	private String dep_name;
	private String address;
	

	//显示用户信息
	public String execute() {
		//得到员工的分页信息
		HttpServletRequest request = ServletActionContext.getRequest();
		int pageSize = 3;
		int pageNo = 0;
		String pageNo_str = request.getParameter("pager.offset");
		if(pageNo_str!=null) {
			pageNo = Integer.parseInt(pageNo_str);
		}
		PageView<Employee> pv = efr.findByPage(Employee.class, "from Employee e where e.flag=1", pageNo, pageSize);
		request.setAttribute("pv", pv);
		//得到父部门的所有信息
		List<Department> deps = dfr.findAll(Department.class, "from Department d where d.parent=null");
		request.setAttribute("deps", deps);
		return "index";
	}
	
	//添加员工信息
	public String addEmp() {
		if(image!=null) {
			String realPath = ServletActionContext.getServletContext().getRealPath("/web/imgs/emp");
			System.out.println("realPath------->\""+realPath+"\"");
			//得到目标文件
			File saveFile = new File(new File(realPath), imageFileName);
			if(!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			try {
				FileUtils.copyFile(image, saveFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			int dep_id = 0;
			if(depId!=null) {
				dep_id = Integer.parseInt(depId);
			}
			Department dep = dfr.findById(Department.class, dep_id);
			emp.setDep(dep);
			emp.setDep_name(dep.getDep_name());
			emp.setFlag(1);
			emp.setEmp_img("web/imgs/emp/"+imageFileName);
			ef.save(emp);
			
		}
		return "addEmp";
	}
	
	//删除员工
	public String deleteEmp() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] strs = request.getParameterValues("cc");
		if(strs != null) {
			for(String str:strs) {
				System.out.println("------->"+str);
				ef.deleteByLogic(Employee.class, new Serializable[]{Integer.parseInt(str)}, "emp_id", "flag");
			}
			return "deleteEmp";
		}
		return "noDeleteItem";
	}
	
	//显示选中员工的详细信息
	public String showEmp() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int empId = 0;
		String str_id = request.getParameter("empId");
		if(str_id!=null) {
		  empId = Integer.parseInt(str_id);	
		}
		emp =  efr.findById(Employee.class, empId);
		request.setAttribute("emp", emp);
		return "showEmp";
	}
	
	//根据用户输入的条件，查找信息
	public String findEmp() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		System.out.println("job+name+dep_name+address------>"+job+name+dep_name+address);
		
		// 放置条件的值
		List<Object> params = new ArrayList<Object>();
		// 放置条件的key
		StringBuffer hql = new StringBuffer();
		hql.append("e.flag=?");
		params.add(new Integer(1));
		if (job != null && !job.equals("") ) {
			if (params.size() > 0) {
				hql.append(" and ");
				hql.append("e.emp_job=?");
				params.add(job.trim());
			}
		}
		
		if (name != null && !name.equals("")) {
			if (params.size() > 0) {
				hql.append(" and ");
				hql.append("e.emp_name=?");
				params.add(name.trim());
			}
		}
		
		if (dep_name != null && !dep_name.equals("")) {
			if (params.size() > 0) {
				hql.append(" and ");
				hql.append("e.dep_name=?");
				params.add(dep_name.trim());
			}
		}
		
		if (address != null && !address.equals("") ) {
			if (params.size() > 0) {
				hql.append(" and ");
				hql.append("e.emp_address=?");
				params.add(address.trim());
			}
		}
		
		int pageSize = 3;
		int pageNo = 0;
		String pageNo_str = request.getParameter("pager.offset");
		if (pageNo_str != null) {
			pageNo = Integer.parseInt(pageNo_str);
		}
		
			
			PageView<Employee> pv = efr.findByPage(Employee.class,
				"from Employee e where " + hql.toString(), params.toArray(),
				pageNo, pageSize);
			request.setAttribute("pv", pv);
		
		
		return "findEmp";
	}

	public File getImage()
	{
		return image;
	}

	public void setImage(File image)
	{
		this.image = image;
	}

	public String getImageFileName()
	{
		return imageFileName;
	}

	public void setImageFileName(String imageFileName)
	{
		this.imageFileName = imageFileName;
	}

	public Employee getEmp()
	{
		return emp;
	}

	public void setEmp(Employee emp)
	{
		this.emp = emp;
	}

	public String getDepId()
	{
		return depId;
	}

	public void setDepId(String depId)
	{
		this.depId = depId;
	}

	public String getJob()
	{
		return job;
	}

	public void setJob(String job)
	{
		this.job = job;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getDep_name()
	{
		return dep_name;
	}

	public void setDep_name(String depName)
	{
		dep_name = depName;
	}

	public String getAddress()
	{
		return address;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	
}
